package cn.net.autocode.platform.handle;

import cn.net.autocode.platform.config.SpringProfileConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
@Slf4j
public class PlatformExceptionHandler implements ErrorController {

	private final SpringProfileConfig springProfileConfig;

    public PlatformExceptionHandler(SpringProfileConfig springProfileConfig) {
        this.springProfileConfig = springProfileConfig;
    }


    @ExceptionHandler(value = Exception.class)
	public ResponseEntity<String> handleAllExceptions(Exception ex) {
		if(springProfileConfig.getProfile().equals("dev")) {
			ex.printStackTrace();
			log.error(ex.getMessage());
		}else{
			log.error(ex.getMessage());
		}

		return new ResponseEntity<>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
	}


}
